﻿using System.IO;
using System.Net;
using System.Xml;
using Baggr.FeedParser;
using FeedReader;

namespace Baggr.Agent
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceProcess;
    using System.Threading;

    public class AgentService
        : ServiceBase
    {
        private Thread _Thread;
        private bool _Ok;
        private ManualResetEvent _WaitHandle;
        /// <summary>
        /// Старт службы.
        /// </summary>
        /// <param name="args">
        /// Параметры коммандной строки </param>
        protected override void OnStart(string[] args)
        {
            OnContinue();
        }
        protected override void OnPause()
        {
            _Ok = false;
            _WaitHandle.WaitOne();
        }

        protected override void OnContinue()
        {
            _WaitHandle = new ManualResetEvent(false);
            _Thread = new Thread(MainTask);
        }

        protected override void OnShutdown()
        {
            _Ok = false;
            _WaitHandle.WaitOne();
        }

        protected void MainTask()
        {
            using(var dc = new FeedDataContext())
            {
                while (_Ok)
                {
                    foreach (var feed in dc.Feeds)
                    {
                        try
                        {
                            var request =WebRequest.Create(feed.Link);
                            Stream stream = request.GetResponse().GetResponseStream();
                            XmlReader reader = XmlReader.Create(stream);
                            var newFeedItemss = reader.GetFeed().Items.Where(curr => feed.Items.FirstOrDefault(exists => exists.LocalId != curr.LocalId) == null);
                            foreach(var feedItem in newFeedItemss)
                            {
                                var newFeed = new FeedItem()
                                                  {
                                                      Author = feedItem.Author,
                                                      Date = feedItem.PublicationDate,
                                                      Description = feedItem.Description,
                                                      Feed = feed,
                                                      Link = feedItem.Link.ToString(),
                                                      LocalId = feedItem.LocalId,
                                                      Title = feedItem.Title
                                                  };
                                feed.Items.Add(newFeed);
                            }
                        }
                        catch
                        {
                        }
                    }
                    dc.SubmitChanges();
                }
            }
            _WaitHandle.Set();
        }
    }
}
